const Db = require('./db')
const vk = require('./vk')

class product
{
  /**
   *
   * 商品列表
   * @static
   * @param {*} ctx
   * @memberof product
   */
  static async list(ctx){
    let params = ctx.query
    let pageStart = (params.page - 1)*params.limit
    let class_id = params.class_id?params.class_id:'',
        title = params.title?params.title:'';
    let data = {
      sql:"SELECT id,pic,title,price,cost,sales,stock,sort,type,class_id,old_price FROM vk_product_list WHERE type = ? AND class_id = CASE WHEN ? != '' THEN ? ELSE class_id END AND title LIKE '%"+ title +"%' LIMIT "+pageStart+","+params.limit,
      values:[params.type,class_id,class_id]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,"请求失败")}
    let countSql = {
      sql:"SELECT COUNT(id) FROM vk_product_list WHERE type = ?",
      values:[params.type]
    }
    let [err2,count] = await Db.query(countSql)
    if(err2){return ctx.body = vk.showData(500,err2,"请求失败")}
    let o = {
      data:res,
      count:count[0]['COUNT(id)']
    }
    ctx.body = vk.showData(200,o,'ok')
  }

  /**
   *
   * 添加商品
   * @static
   * @param {*} ctx
   * @returns
   * @memberof product
   */
  static async add(ctx){
    let params = ctx.request.body
    let list = JSON.parse(params.list)
    list = list.sort((a,b)=>a-b)[0]
    let price = list.price,
        cost = list.cost_price,
        old_price = list.old_price;
    let add_spec = {
      sql:"INSERT INTO vk_product_spec_list (list,tmp) VALUES (?,?)",
      values:[params.list,params.tmp]
    }
    let [err2,res2] = await Db.query(add_spec)
    if(err2){return ctx.body = vk.showData(500,err2,'请求失败')}
    let last_insert_id = res2.insertId
    let data = {
      sql:"INSERT INTO vk_product_list (pic,title,price,old_price,cost,sales,stock,sort,type,content,spec_type,class_id,spec_id) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)",
      values:[params.pic,params.title,price,old_price,cost,params.sales,params.stock,params.sort,params.type,params.content,params.class_id,params.spec_type,last_insert_id]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求失败')}
    ctx.body = vk.showData(200,res,'ok')
  }

  static async changeType(ctx){
    let params = ctx.request.body
    let data = {
      sql:"UPDATE vk_product_list SET type = ? WHERE id = ?",
      values:[params.type,params.id]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求失败')}
    ctx.body = vk.showData(200,res,'ok')
  }

  static async detail(ctx){
    let params = ctx.query
    let data = {
      sql:"SELECT a.*,b.* FROM vk_product_list a INNER JOIN vk_product_spec_list b ON a.id = ? AND a.spec_id = b.id",
      values:[params.id]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求失败')}
    ctx.body = vk.showData(200,res,'ok')
  }

  /**
   *
   * 修改商品
   * @static
   * @param {*} ctx
   * @returns
   * @memberof product
   */
  static async edit(ctx){
    let params = ctx.request.body
    let list = JSON.parse(params.list)
    let price = 0,
        cost = 0,
        old_price = 0;
    if(list.length != 0){
      list = list.sort((a,b)=>a-b)[0]
      price = list.price,
      cost = list.cost_price,
      old_price = list.old_price;
    }
    let specEdit = {
      sql:"UPDATE vk_product_spec_list SET list = ?,tmp = ? WHERE id = ?",
      values:[params.list,params.tmp,params.spec_id]
    }
    let [err2,res2] = await Db.query(specEdit)
    if(err2){return ctx.body = vk.showData(500,err2,'请求失败')}
    let data = {
      sql:"UPDATE vk_product_list SET pic = ?,title = ?,price = ?,old_price = ?,cost = ?,sales = ?,stock = ?,sort = ?,type = ?,content = ?,spec_type = ?,class_id = ? WHERE id = ?",
      values:[params.pic,params.title,price,old_price,cost,params.sales,params.stock,params.sort,params.type,params.content,params.spec_type,params.class_id,params.id]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求失败')}
    ctx.body = vk.showData(200,res,'ok')
  }

  static async getClass(ctx){
    let params = ctx.query
    let id = params.id?params.id:''
    let keyword = params.keyword?params.keyword:''
    let data = {
      sql:"SELECT * FROM vk_product_classify WHERE id = CASE WHEN ? != '' THEN ? ELSE id END AND title LIKE '%"+keyword.replace(/\s/g,"")+"%'",
      values:[id,id]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求错误')}
    ctx.body = vk.showData(200,res,'ok')
  }

  static async classAdd(ctx){
    let params = ctx.request.body
    let data = {
      sql:"INSERT INTO vk_product_classify (title,sort,pid,pic,is_show) VALUES (?,?,?,?,?)",
      values:[params.title,params.sort,params.pid,params.pic,params.is_show]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求错误')}
    ctx.body = vk.showData(200,res,'ok')
  }

  static async classEdit(ctx){
    let params = ctx.request.body
    let data = {
      sql:"UPDATE vk_product_classify SET title = ?,sort = ?,pid = ?,pic = ?,is_show = ? WHERE id = ?",
      values:[params.title,params.sort,params.pid,params.pic,params.is_show,params.id]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求错误')}
    ctx.body = vk.showData(200,res,'ok')
  }

  static async classDel(ctx){
    let params = ctx.request.body
    let data = {
      sql:"DELETE FROM vk_product_classify WHERE id = ?",
      values:[params.id]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求错误')}
    ctx.body = vk.showData(200,res,'ok')
  }

  static async reply(ctx){
    let params = ctx.query
    let data = {
      sql:"SELECT a.*,b.title AS product_name,b.pic AS product_pic FROM vk_product_reply a INNER JOIN vk_product_list b WHERE a.product_id = b.id AND a.time >= CASE WHEN ? != '' THEN ? ELSE a.time END AND a.time <= CASE WHEN ? != '' THEN ? ELSE a.time END AND ((b.title LIKE CASE WHEN ? != '' THEN '%"+ params.keyWord.replace(/\s/g,"") +"%' ELSE b.title END AND ? = 1) OR (b.id = '"+ params.keyWord.replace(/\s/g,"") +"' AND ? = 0)) AND a.username = CASE WHEN ? != '' THEN ? ELSE a.username END AND ? = CASE WHEN ? != '' THEN (CASE WHEN a.reply != '' THEN 1 ELSE 0 END) ELSE '' END ",
      values:[params.starttime,params.starttime,params.endtime,params.endtime,params.keyWord,params.is_word,params.is_word,params.username,params.username,params.is_reply,params.is_reply]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求错误')}
    ctx.body = vk.showData(200,res,'ok')
  }

  static async replyAdd(ctx){
    let params = ctx.request.body
    let data = {
      sql:"INSERT INTO vk_product_reply (username,product_id,eval_content,eval_pic,score_product,score_serve,time) VALUES (?,?,?,?,?,?,?)",
      values:[params.username,params.product_id,params.eval_content,params.eval_pic,params.score_product,params.score_serve,Math.floor((new Date()).valueOf()/1000)]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求错误')}
    ctx.body = vk.showData(200,res,'ok')
  }

  static async replyHf(ctx){
    let params = ctx.request.body
    let data = {
      sql:"UPDATE vk_product_reply SET reply = ? WHERE id = ?",
      values:[params.reply,params.id]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求错误')}
    ctx.body = vk.showData(200,res,'ok')
  }

  static async replyDel(ctx){
    let params = ctx.request.body
    let data = {
      sql:"DELETE FROM vk_product_reply WHERE id = ?",
      values:[params.id]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求错误')}
    ctx.body = vk.showData(200,res,'ok') 
  }

  static async specList(ctx){
    let params = ctx.query
    params.keyword = params.keyword?params.keyword:''
    let data = {
      sql:"SELECT * FROM vk_product_spec WHERE name LIKE CASE WHEN ? != '' THEN '%"+params.keyword+"%' ELSE name END",
      values:[params.keyword]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求错误')}
    ctx.body = vk.showData(200,res,'ok')
  }

  static async specAdd(ctx){
    let params = ctx.request.body
    let data = {
      sql:"INSERT INTO vk_product_spec (`name`,data) VALUES (?,?)",
      values:[params.name,params.data]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求错误')}
    ctx.body = vk.showData(200,res,'ok')
  }

  static async specEdit(ctx){
    let params = ctx.request.body
    let data = {
      sql:"UPDATE vk_product_spec SET `name` = ?,data = ? WHERE id = ?",
      values:[params.name,params.data,params.id]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求错误')}
    ctx.body = vk.showData(200,res,'ok')
  }

  static async specDel(ctx){
    let params = ctx.request.body
    let data = {
      sql:"DELETE FROM vk_product_spec WHERE id IN ("+params.id+")",
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求错误')}
    ctx.body = vk.showData(200,res,'ok')
  }
}

module.exports = product